package com.dongdongshop.serviceimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationExample;
import com.dongdongshop.pojo.TbSpecificationOptionExample;
import com.dongdongshop.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
@org.springframework.stereotype.Service
public class SpecificationServiceImpl implements SpecificationService {
    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;
    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;
    //查询所有规格管理信息
    @Override
    public List<TbSpecification> findAll(String specName) {
        TbSpecificationExample tse = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = tse.createCriteria();
        criteria.andSpecNameLike("%" + specName + "%");
        List<TbSpecification> tbSpecifications = tbSpecificationMapper.selectByExample(tse);
        return tbSpecifications;
    }

    //查询规格名称
    @Override
    public TbSpecification selectSpecById(Long id) {
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
        return tbSpecification;
    }
    
    //删除
    @Override
    public Boolean deleteById(Long[] ids) {

        try {
            TbSpecificationOptionExample toe = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = toe.createCriteria();
            for (int i = 0; i < ids.length; i++) {
                //删除规格
                tbSpecificationMapper.deleteByPrimaryKey(ids[i]);
                //删除规格型号
                criteria.andSpecIdEqualTo(ids[i]);
                tbSpecificationOptionMapper.deleteByExample(toe);
            }



            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
